package com.trace.meat.controller;

import com.trace.meat.entity.*;
import com.trace.meat.entity.Process;
import com.trace.meat.entityModel.*;
import com.trace.meat.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.relational.core.sql.In;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//http://127.0.0.1:11451/ent/selectLine?offset=0&limit=3
//http://127.0.0.1:11451/ent/insertOne?ent=asd
//http://127.0.0.1:11451/ent/login?username=qwe&password=123
/**
 * (Ent)表控制层
 *
 * @author makejava
 * @since 2021-06-19 11:17:26
 */
@RestController
@RequestMapping("ent")
public class EntController {
    /**
     * 服务对象
     */
    @Resource
    private EntService entService;
    @Resource
    private ButcherService butcherService;
    @Resource
    private BreederController breederController;
    @Resource
    private ButcherController butcherController;
    @Resource
    private LicController licController;
    @Resource
    private AnimalController animalController;
    @Resource
    private ProductController productController;
    @Resource
    private AnimalService animalService;
    @Resource
    private ProductService productService;
    @Resource
    private BreederService breederService;
    @Resource
    private LicService licService;
    @Resource
    private OrderTblService orderTblService;
    @Resource
    private ProcessService processService;
    @Resource
    private TypeTblService typeTblService;
    @Resource
    private StockService stockService;
    //账号管理
    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return Result对象
     */
    @GetMapping( "login")
    public Result login(String username,String password) {
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        System.out.println(username);
        System.out.println(password);
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        if (username==null||username=="")
        {
            result.setMsg("用户名不可为空");
            System.out.println("用户名不可为空");
            return result;
        }
        if(password==""||password==null)
        {
            result.setMsg("密码不可为空");
            System.out.println("密码不可为空");
            return result;

        }
        Ent temp=new Ent();
        temp=this.entService.queryByUserName(username);
        if(temp==null) {
            System.out.println("用户名不存在");
            result.setMsg("用户名不存在");
            return result;
        }
        if(temp.getToken()==null)
        {
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            temp.setToken(uuid);
            this.entService.update(temp);
        }
        boolean flag=bCryptPasswordEncoder.matches(password,temp.getPwd());
        if(flag)
        {
            temp.setPwd("");
            System.out.println("登录成功");
            result.setMsg("登录成功");
            result.setSuccess(true);
            result.setDetail(temp);
            return result;
        }
        else
        {
            result.setMsg("密码错误");
            System.out.println("密码错误");
            return result;
        }
    }
    /**
     * 注册
     *
     * @param username 用户名
     * @param password 密码
     * @param etype 企业类型
     * @param tele 电话
     * @param ename 企业名
     * @param email 邮箱名
     * @return Result对象
     */
    @PostMapping("register")
    public Result register(String username,String password,Integer etype,String tele,String ename,String email) {
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        Result result=new Result();
        Ent temp=new Ent();
        result.setSuccess(false);
        result.setDetail(null);
        temp=this.entService.queryByUserName(username);
        if (username==null||username.equals("")) {
            result.setMsg("用户名不可为空");
            System.out.println("用户名不可为空");
            return result;
        }
        if (temp!=null) {
            result.setMsg("用户名重复");
            System.out.println("用户名重复");
            return result;
        }
        String regEx = "^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4})*$";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher ;
        if(email!=null) {
            //邮箱格式验证
            matcher = pattern.matcher(email);
            if (!matcher.matches()) {
                result.setMsg("邮箱格式错误");
                System.out.println("邮箱格式错误");
                return result;
            }
        }
        //用户名格式验证
        regEx="^[\\w\\d_]{4,16}$";
        pattern=Pattern.compile(regEx);
        matcher=pattern.matcher(username);
        if(!matcher.matches()){
            result.setMsg("用户名格式错误");
            System.out.println("用户名格式错误");
            return result;
        }
        //密码格式验证
        regEx="^[\\w\\d~!@#$%^&*_.-]{8,64}$";
        pattern=Pattern.compile(regEx);
        matcher=pattern.matcher(password);
        if(!matcher.matches()){
            result.setMsg("密码格式错误");
            System.out.println("密码格式错误");
            return result;
        }
        temp=new Ent();
        temp.setUsername(username);
        password=bCryptPasswordEncoder.encode(password);
        temp.setPwd(password);
        temp.setEtype(etype);
        temp.setEname(ename);
        temp.setTel(tele);
        String token=UUID.randomUUID().toString().replaceAll("-","");
        temp.setToken(token);
        temp.setEmail(email);
        this.entService.insert(temp);
        temp.setPwd("");
        result.setDetail(temp);
        result.setMsg("注册成功");
        result.setSuccess(true);
        System.out.println("注册成功");
        return result;
    }
    /**
     * 更新
     * @param
     * @return
     */
    @PostMapping("editDetail")
    public Result update(Integer eid,Integer etype,String tele,String ename,String email) {
        //邮箱格式验证
        Result result=new Result();
        result.setSuccess(false);
        String regEx="^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4})*$";
        Pattern pattern=Pattern.compile(regEx);
        Matcher matcher;
        if(email!=null){
            matcher=pattern.matcher(email);
        if(!matcher.matches()){
            result.setMsg("邮箱格式错误");
            System.out.println("邮箱格式错误");
            return result;
        }
        }
        Ent ent=new Ent();
        ent.setEid(eid);
        ent.setEtype(etype);
        ent.setTel(tele);
        ent.setEname(ename);
        result.setSuccess(true);
        result.setMsg("修改成功");
        ent=entService.update(ent);
        ent.setPwd("");
        result.setDetail(ent);
        return result;
    }
    /**
     * 修改密码
     * @param
     * @return
     */
    @PostMapping("editPass")
    public Result editPassword(Integer eid,String oldPass,String newPass) {
        //邮箱格式验证
        Result result=new Result();
        result.setSuccess(false);
        String regEx="^[\\w\\d~!@#$%^&*_.-]{8,64}$";
        Pattern pattern=Pattern.compile(regEx);
        Matcher matcher=pattern.matcher(newPass);
        if(!matcher.matches()){
            result.setMsg("新密码格式错误");
            System.out.println("新密码格式错误");
            return result;
        }
        Ent ent=entService.queryById(eid);
        if (ent==null){
            result.setMsg("企业编号错误");
            System.out.println("企业编号错误");
            return result;
        }
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        if(!bCryptPasswordEncoder.matches(oldPass,ent.getPwd())) {
            result.setMsg("旧密码错误");
            System.out.println("旧密码错误");
            return result;
        }
        String token=UUID.randomUUID().toString().replaceAll("-","");
        ent.setToken(token);
        ent.setPwd(bCryptPasswordEncoder.encode(newPass));
        ent=entService.update(ent);
        ent.setPwd("");
        result.setSuccess(true);
        result.setMsg("修改成功");
        result.setDetail(ent);
        return result;
    }
    //新增数据部分
    /**
     * 新增饲养员数据
     *
     * @param bname 饲养员名字
     * @param  eid 企业id
     * @return 单条数据
     */
    @PostMapping("breeder/add")
    public Result AddBreeder(String bname,Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        if(entService.queryById(eid)==null)
        {
            result.setMsg("企业不存在");
            return result;
        }
        result.setDetail(breederController.insert(bname,eid));
        result.setSuccess(true);
        result.setMsg("添加饲养员成功");
        return result;
    }
    /**
     * 新增牲畜经营者数据
     *
     * @param bname 牲畜经营者名字
     * @param  eid 企业id
     * @return Result对象
     */
    @PostMapping("butcher/add")
    public Result AddButcher(String bname, Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        if(entService.queryById(eid)==null)
        {
            result.setMsg("企业不存在");
            return result;
        }
        result.setDetail(butcherController.insert(bname,eid));
        result.setSuccess(true);
        result.setMsg("添加牲畜经营者成功");
        return result;
    }
    /**
     * 新增牲畜记录
     *
     * @param  type 类型
     * @param  breed 品种
     * @param  bid 饲养员id
     * @return 单条数据
     */
    @PostMapping("animal/add")
    public Result AddAnimal(Integer type, Integer breed,Integer bid) {
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        if(breederService.queryById(bid)==null){
            result.setMsg("饲养员不存在");
            return result;
        }
        Date date=new Date();
        short state=0;//初始
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer lastoid=null;
        String time=sdf.format(date);
        Animal animal=animalController.Insert(time,type,breed,bid,lastoid,state);
        if(animal==null)
        {
            result.setMsg("信息错误");
            return result;
        }
        result.setMsg("加入成功");
        result.setSuccess(true);
        result.setDetail(animal);
        return result;
    }
    /**
     * 插入新产品数据
     *
     * @param ptype 肉品类型
     * @param weight 重量
     * @param bid 屠宰着id
     * @param aid 牲畜id
     * @return Result对象
     */
    @PostMapping("process/add")
    public Result AddProduct(Integer ptype, Double weight,  Integer bid,Integer aid){
        Result result=new Result();
        result.setSuccess(false);
        if(butcherService.queryById(bid)==null){
            result.setMsg("经营者不存在");
            return result;
        }
        if(animalService.queryById(aid)==null){
            result.setMsg("牲畜不存在");
            return result;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=sdf.format(new Date());
        Short state=0;
        Integer lastoid=animalService.queryById(aid).getLastOid();
        return this.productController.insert(ptype,weight,time,lastoid,state,bid,aid);
    }
    /**
     * 新增证书数据
     *
     * @param  eid 企业id
     * @param serNum 证书编号
     * @param licType 证书类型
     * @return Result对象
     */
    @PostMapping("lic/add")
    public Result AddLic(Integer eid,String serNum,Integer licType){
        Result result=new Result();
        Integer licState=0;
        result.setSuccess(false);
        if(entService.queryById(eid)==null){
            result.setMsg("企业不存在");
            return result;
        }
        result.setDetail(licController.insert(eid,serNum,licType,licState));
        result.setSuccess(true);
        result.setMsg("添加证书信息成功");
        return result;
    }
    /**
     * 上架
     * @param eid 主键
     * @param semp 上架人编号
     * @param gid 货物编号
     * @param sid 货物编号
     * @return
     */
    @PostMapping("stock/add")
    public Result AddStock(Integer eid,Integer semp,Integer gid,Integer sid){
        Result result=new Result();
        result.setSuccess(false);
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=sdf.format(date);
        if(entService.queryById(eid)==null){
            result.setMsg("企业不存在");
            return result;
        }
        if(productService.queryById(gid)==null){
            result.setMsg("产品不存在");
            return result;
        }
        Stock stock=new Stock();
        stock.setEid(eid);
        stock.setStime(time);
        stock.setSemp(semp);
        stock.setGid(gid);
        stock.setSid(sid);
        stockService.insert(stock);
        result.setDetail(stock);
        result.setSuccess(true);
        result.setMsg("添加货架信息成功");
        return result;
    }
    //查询部分
    /**
     * 通过eid查询单条数据
     *
     * @param eid 主键
     * @return Result对象
     */
    @GetMapping("detail")
    @Transactional(readOnly = true)
    public Result queryModelById(Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        Ent entModel=entService.queryById(eid);
        if(entModel==null){
            System.out.println("企业编号错误");
            result.setMsg("企业编号错误");
            return result;
        }
        entModel.setPwd("");
        result.setDetail(entModel);
        result.setSuccess(true);
        result.setMsg("查询信息成功");
        return result;
    }
    /**
     * 返回本地时间
     * @return 本地时间
     */
    @GetMapping("getTime")
    public Result queryTime(){
        Result result=new Result();
        result.setSuccess(true);
        result.setMsg("时间获取成功");
        long date=new Date().getTime();
        result.setDetail(date);
        return result;
    }
    /**
     * 查询饲养员数据
     *
     * @param  eid 饲养员id
     * @return Result对象
     */
    @GetMapping("breeder/list")
    @Transactional(readOnly = true)
    public Result SelectAllBreeder(Integer eid){
        System.out.println(eid);
        Result result=new Result();
        result.setSuccess(false);
        if(entService.queryById(eid)==null) {
            result.setMsg("企业不存在");
            result.setDetail(null);
            return result;
        }
        List<BreederModel> list=breederService.queryModelByEId(eid);
        if(list.isEmpty()){
            result.setMsg("该企业下无饲养员信息");
            result.setDetail(null);
            return result;
        }
        result.setDetail(list);
        result.setMsg("查询饲养员信息成功");
        result.setSuccess(true);
        return result;
    }
    @GetMapping("process/list")
    @Transactional(readOnly = true)
    public Result SelectALlProcess(Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        if(entService.queryById(eid)==null) {
            result.setMsg("企业不存在");
            result.setDetail(null);
            return result;
        }
        List<ButcherModel> list=butcherService.queryModelByEId(eid);
        if(list.isEmpty()){
            result.setMsg("该企业下无经营者信息");
            result.setDetail(null);
            return result;
        }
        Iterator iterator=list.listIterator();
        List<Process> list_process=new ArrayList<>();
        while (iterator.hasNext()) {
            ButcherModel temp=(ButcherModel)iterator.next();
            List<Process> list1 = processService.queryAllByBid(temp.getBid());
            list_process.addAll(list1);
        }
        result.setDetail(list_process);
        result.setSuccess(true);
        result.setMsg("加工信息获取成功");
        return result;
    }
    /**
     * 查询经营者数据
     *
     * @param  eid 饲养员id
     * @return Result对象
     */
    @GetMapping("butcher/list")
    @Transactional(readOnly = true)
    public Result SelectAllButcher(Integer eid){
        System.out.println(eid);
        Result result=new Result();
        result.setSuccess(false);
        if(entService.queryById(eid)==null) {
            result.setMsg("企业不存在");
            result.setDetail(null);
            return result;
        }
        List<ButcherModel> list=butcherService.queryModelByEId(eid);
        if(list.isEmpty()){
            result.setMsg("该企业下无经营者信息");
            result.setDetail(null);
            return result;
        }
        result.setDetail(list);
        result.setMsg("查询经营者信息成功");
        result.setSuccess(true);
        return result;
    }
    /**
     * 查询货架数据
     *
     * @param  eid 饲养员id
     * @return Result对象
     */
    @GetMapping("stock/list")
    public Result SelectAllStock(Integer eid){
        System.out.println(eid);
        Result result=new Result();
        result.setSuccess(false);
        if(entService.queryById(eid)==null) {
            result.setMsg("企业不存在");
            result.setDetail(null);
            return result;
        }
        List<Stock> list=stockService.queryAllByEId(eid);
        if(list.isEmpty()){
            result.setMsg("该企业下无货架信息");
            result.setDetail(null);
            return result;
        }
        result.setDetail(list);
        result.setMsg("查询货架记录信息成功");
        result.setSuccess(true);
        return result;
    }
    /**
     * 查询牲畜数据
     * @param  eid 饲养员id
     * @return Result对象
     */
    @GetMapping("animal/list")
    @Transactional(readOnly = true)
    public Result SelectAllAnimal(Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        if(entService.queryById(eid)==null) {
            result.setMsg("企业不存在");
            result.setDetail(null);
            return result;
        }
        List<BreederModel> list_breeder=breederService.queryModelByEId(eid);
        if(list_breeder.isEmpty()){
            result.setMsg("该企业下无饲养员信息");
            result.setDetail(null);
            return result;
        }
        Iterator iterator=list_breeder.listIterator();
        List<AnimalModel> list_animal=new ArrayList<AnimalModel>();
        while (iterator.hasNext()) {
            BreederModel temp=(BreederModel) iterator.next();
            List<AnimalModel> list=animalService.queryModelByBId(temp.getBid());
            list_animal.addAll(list);
        }
        result.setDetail(list_animal);
        result.setMsg("查询牲畜信息成功");
        result.setSuccess(true);
        return result;
    }
    /**
     * 查询牲畜数据
     * @param  eid 主键
     * @return Result对象
     */
    @GetMapping("lic/list")
    @Transactional(readOnly = true)
    public Result SelectAllLic(Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        if(entService.queryById(eid)==null) {
            result.setMsg("企业不存在");
            result.setDetail(null);
            return result;
        }

        List<LicModel> list_lic=licService.getModelByEId(eid);
        if(list_lic.isEmpty()){
            result.setMsg("该企业下无证书信息");
            result.setDetail(null);
            return result;
        }
        result.setDetail(list_lic);
        result.setMsg("查询证书信息成功");
        result.setSuccess(true);
        return result;
    }
    //类型获取
    /**
     * 获取某种牲畜下所有产品类型
     * @return Result对象
     */
    @GetMapping("SelectAllProductType")
    public Result SelectAllProductType(Integer type){
        Result result=new Result();
        Integer left=type*1000+1;
        Integer right=type*1000+999;
//        Integer mid=(left+right)/2;
//        while (left<right){
//            if(typeTblService.queryById(mid)!=null){
//                right=(right+mid)/2;
//                if(typeTblService.queryById(right)==null){break;}
//            }
//            else{
//                right=mid;
//            }
//            mid=(left+right)/2;
//        }
        result.setDetail(typeTblService.queryAllById(left,right));
        result.setSuccess(true);
        result.setMsg("查询产品类型成功");
        return result;
    }
    /**
     * 获取某种牲畜下所有品种类型
     * @return Result对象
     */
    @GetMapping("SelectAllBreedType")
    public Result SelectAllBreedType(Integer type){
        Result result=new Result();
        Integer left=type*10000+1;
        Integer right=type*10000+9999;
//        Integer mid=(left+right)/2;
//        while (typeTblService.queryById(right+1)==null){
//            if(typeTblService.queryById(mid)!=null){
//                right=(right+mid)/2;
//                if(typeTblService.queryById(right)==null){break;}
//            }
//            else{
//                right=mid;
//            }
//            mid=(left+right)/2;
//        }
        result.setDetail(typeTblService.queryAllById(left,right));
        result.setSuccess(true);
        result.setMsg("查询品种类型成功");
        return result;
    }
    /**
     * 获取全部牲畜类型
     * @return Result对象
     */
    @GetMapping("SelectAllAnimalType")
    public Result SelectAllAnimalType(){
        Result result=new Result();
//        Integer left=5001;
//        Integer right=5999;
////        Integer mid=(left+right)/2;
////        while (left<right){
////            if(typeTblService.queryById(mid)!=null){
////                right=(right+mid)/2;
////                if(typeTblService.queryById(right)==null){break;}
////            }
////            else{
////                right=mid;
////            }
////            mid=(left+right)/2;
////        }
        result.setDetail(typeTblService.queryAllById(5001,5999));
        result.setMsg("获取全部牲畜类型成功");
        result.setSuccess(true);
        return result;
    }
    /**
     * 获取全部企业类型
     * @return Result对象
     */
    @GetMapping("SelectAllEntType")
    public Result SelectAllEntType(){
        Result result=new Result();
        Integer left=100;
        Integer right=5000;
        Integer mid=(left+right)/2;
        while (left<right){
            if(typeTblService.queryById(mid)!=null){
                left=mid+1;
            }
            else{
                right=mid;
            }
            mid=(left+right)/2;
        }
        result.setDetail(typeTblService.queryAllById(100,mid));
        result.setSuccess(true);
        result.setMsg("企业类型获取成功");
        return result;
    }
    /**
     * 获取全部证书类型
     * @return Result对象
     */
    @GetMapping("SelectAllLicType")
    public Result SelectAllLicType(){
        Result result=new Result();
        Integer left=1;
        Integer right=100;
        Integer mid=(left+right)/2;
        while (left<right){
            if(typeTblService.queryById(mid)!=null){
                left=mid+1;
            }
            else{
                right=mid;
            }
            mid=(left+right)/2;
        }
        result.setDetail(typeTblService.queryAllByLimit(1,mid));
        result.setMsg("获取全部证书类型成功");
        result.setSuccess(true);
        return result;
    }
    /**
     * 获取全部类型
     * @return Result对象
     */
    @GetMapping("SelectAllType")
    public Result SelectAlltype(){
        Result result=new Result();
        List<TypeTbl> typeTbl=typeTblService.queryAllType();
        result.setSuccess(true);
        result.setMsg("获取全部类型成功");
        result.setDetail(typeTbl);
        return result;
    }
    /**
     * 删除证书
     * @param lid 证书编号
     * @return Result
     */
    @PostMapping("licdelete")
    public Result Lic_Delete(Integer lid){
        Result result=new Result();
        result.setSuccess(false);
        if(licService.deleteById(lid)) {
            result.setSuccess(true);
            result.setMsg("删除成功");
            return result;
        }
        else {
            result.setMsg("删除失败，证书不存在");
            return result;
        }
    }
    //订单相关
    /**
     * 收货
     * @param eid 主键
     * @param gid 货物id
     * @param gtype 货物类型
     * @param plate 车牌
     * @param info 其他信息
     * @return Result对象
     */
    @PostMapping("order/confirm")
    @Transactional
    public Result confirm(Integer eid,Integer gid,Integer gtype,String plate,String info){
        Result result=new Result();
        result.setSuccess(false);
        if(gtype==0)//整猪
        {
            Animal animal=animalService.queryById(gid);
            if(animal==null){
                result.setMsg("牲畜不存在，货物id或货物类型错误");
                System.out.println("牲畜不存在，货物id或货物类型错误");
                return result;
            }
            EntModel entModel=LastPosition(gid,gtype);
            if(entModel==null) {
                result.setMsg("货物无企业信息");
                return result;
            }
            if((int)entModel.getEid()==(int)eid){
                result.setMsg("不可同一企业收货");
                return result;
            }
            Integer outeid=entModel.getEid();
            OrderTbl neworder=new OrderTbl();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time=sdf.format(new Date());
            neworder.setPlate(plate);
            neworder.setOutEid(outeid);
            neworder.setInEid(eid);
            neworder.setGid(gid);
            neworder.setGtype((short)0);
            neworder.setInfo(info);
            neworder.setConfirmTime(time);
            if(animal.getLastOid()!=null){
                neworder.setLastOid(animal.getLastOid());
            }
            OrderTbl orderTbl=orderTblService.insert(neworder);
            result.setDetail(orderTbl);
            result.setMsg("收货成功");
            animal.setLastOid(orderTbl.getOid());
            animalService.update(animal);
            result.setSuccess(true);
        }
        else if(gtype==1)//切割
        {
            Product product=productService.queryById(gid);
            if(product==null){
                result.setMsg("货物不存在，货物id或货物类型错误");
                System.out.println("货物不存在，货物id或货物类型错误");
                return result;
            }
            EntModel entModel=LastPosition(gid,gtype);
            if(entModel==null) {
                result.setMsg("货物无企业信息");
                return result;
            }
            if((int)entModel.getEid()==(int)eid){
                result.setMsg("不可同一企业收货");
                return result;
            }
            Integer outeid=entModel.getEid();
            OrderTbl neworder=new OrderTbl();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time=sdf.format(new Date());
            neworder.setPlate(plate);
            neworder.setOutEid(outeid);
            neworder.setInEid(eid);
            neworder.setGid(gid);
            neworder.setGtype((short)1);
            neworder.setInfo(info);
            neworder.setConfirmTime(time);
            neworder.setLastOid(product.getLastOid());
            OrderTbl orderTbl=orderTblService.insert(neworder);
            result.setDetail(orderTbl);
            result.setSuccess(true);
            product.setLastOid(orderTbl.getOid());
            productService.update(product);
            result.setMsg("收货成功");
        }
        else{
            result.setMsg("货物类型错误");
            return result;
        }
        return result;
    }
    /**
     * 通过EID查询多条收货订单
     *
     * @param eid 主键
     * @return 实例对象
     */
    @GetMapping("order/listIn")
    @Transactional(readOnly = true)
    public Result List_In(Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        List<OrderTblModel> list=orderTblService.queryModelByInEId(eid);
        if(list.isEmpty()){
            result.setMsg("该企业下无订单");
            return result;
        }
        result.setDetail(list);
        result.setSuccess(true);
        result.setMsg("收货订单查询成功");
        return result;
    }
    /**
     * 通过EID查询多条出货订单
     *
     * @param eid 主键
     * @return 实例对象
     */
    @GetMapping("order/listOut")
    @Transactional(readOnly = true)
    public Result List_Out(Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        List<OrderTblModel> list=orderTblService.queryModelByOutEId(eid);
        if(list.isEmpty()){
            result.setMsg("该企业下无订单");
            return result;
        }
        result.setDetail(list);
        result.setSuccess(true);
        result.setMsg("出货订单查询成功");
        return result;
    }
    /**
     * 获取货物最后公司
     * @param gid 货物id
     * @param gtype 货物类型
     * @return Result对象
     */
    @GetMapping("order/lastPostion")
    @Transactional(readOnly = true)
    public Result lastPosition(Integer gid,Integer gtype){
        Result result=new Result();
        result.setSuccess(false);
        if(gtype==0)//整猪
        {
            Animal animal=animalService.queryById(gid);
            if(animal==null){
                result.setMsg("货物id或类型错误");
                return result;
            }
            if(animal.getLastOid()==null){
                EntModel entModel=entService.queryModelById((breederService.queryById(animal.getBid())).getEid());
                entModel.setPwd("");
                result.setDetail(entModel);
                result.setSuccess(true);
                result.setMsg("货物信息获取成功");
            }
            else {
                EntModel entModel=entService.queryModelById((orderTblService.queryById(animal.getLastOid())).getInEid());
                entModel.setPwd("");
                result.setDetail(entModel);
                result.setSuccess(true);
                result.setMsg("货物信息获取成功");
            }
            return result;
        }
        else if(gtype==1)//切割
        {
            Product product=productService.queryById(gid);
            if(product==null){
                result.setMsg("货物id或类型错误");
                return result;
            }
            result.setMsg("货物最后位置获取成功");
            result.setSuccess(true);
            result.setDetail(entService.queryModelById((orderTblService.queryById(product.getLastOid())).getInEid()));
            return result;
        }
        return result;
    }
    /**
     * 获取货物最后公司
     * @param gid 货物id
     * @param gtype 货物类型
     * @return Result对象
     */
    @Transactional(readOnly = true)
    public EntModel LastPosition(Integer gid,Integer gtype){
        if(gtype==0)//整猪
        {
            Animal animal=animalService.queryById(gid);
            if(animal==null){
                return null;
            }
            if(animal.getLastOid()==null){
                return entService.queryModelById((breederService.queryById(animal.getBid())).getEid());
            }
            else {
                return entService.queryModelById((orderTblService.queryById(animal.getLastOid())).getInEid());
            }
        }
        else if(gtype==1)//切割
        {
            Product product=productService.queryById(gid);
            if(product==null){
                return null;
            }
            return entService.queryModelById((orderTblService.queryById(product.getLastOid())).getInEid());
        }
        return null;
    }
    //url部分结束
    /**
     * 通过主键查询单条数据
     *
     * @param eid 主键
     * @return 单条数据
     */
    public Ent selectOne(Integer eid) {
        return this.entService.queryById(eid);
    }
    /**
     * 通过token查询单条数据
     *
     * @param token token
     * @return 实例对象
     */
    public Ent queryByToken(String token){
        System.out.println("token查询成功");
        return  this.entService.queryByToken(token);}
    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    public List<Ent> selectLine(@Param("offset") int offset, @Param("limit") int limit) {
        return this.entService.queryAllByLimit(offset,limit);
    }
    /**
     * 新增数据
     *
     * @param ent 实例对象
     * @return 实例对象
     */
    public Ent insert(Ent ent) {
        return this.entService.insert(ent);
    }
    public boolean deleteById(Integer eid) {
        return this.entService.deleteById(eid);
    }
    /**
     * 查询牲畜数据
     *
     * @param  aid 牲畜id
     * @return Result对象
     */
    @Transactional
    public Result SelectAnimal(Integer aid){
        Result result=new Result();
        result.setSuccess(false);
        Animal animal=animalService.queryById(aid);
        if(animal==null){
            result.setMsg("牲畜不存在");
            result.setDetail(null);
            return result;
        }
        result.setDetail(animal);
        result.setMsg("查询牲畜信息成功");
        result.setSuccess(true);
        return result;
    }
    /**
     * 查询产品数据
     *
     * @param  pid 产品id
     * @return Result对象
     */
    public Result SelectProduct(Integer pid){
        Result result=new Result();
        result.setSuccess(false);
        Product product=productService.queryById(pid);
        if(product==null){
            result.setMsg("牲畜不存在");
            result.setDetail(null);
            return result;
        }
        result.setDetail(product);
        result.setMsg("查询牲畜信息成功");
        result.setSuccess(true);
        return result;
    }
    /**
     * 查询牲畜经营者数据
     *
     * @param  bid 经营者id
     * @return Result对象
     */
    public Result SelectButcher(Integer bid){
        Result result=new Result();
        result.setSuccess(false);
        Butcher butcher=butcherController.selectOne(bid);
        if(butcher==null){
            result.setMsg("牲畜不存在");
            result.setDetail(null);
            return result;
        }
        result.setDetail(butcher);
        result.setMsg("查询牲畜信息成功");
        result.setSuccess(true);
        return result;
    }

    /**
     * 更新牲畜数据
     * @param  id 主键
     * @param  time 出生时间
     * @param  type 类型
     * @param  breed 品种
     * @param  bid 饲养员id
     * @param  lastoid 最新订单号
     * @param  state 出生时间
     * @return 单条数据
     */
    public Result UpdateAnimal(Integer id,String time, Integer type, Integer breed,Integer bid,Integer lastoid,short state){
        Result result=new Result();
        result.setSuccess(false);
        if(animalService.queryById(id)==null){
            result.setMsg("牲畜id错误");
            return result;
        }
        Animal temp=new Animal();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(time);
        } catch (ParseException e) {
            System.out.println("时间输入格式错误");
            result.setMsg("时间输入格式错误");
            return result;
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
        }
        temp.setBirth(time);
        temp.setType(type);
        temp.setBreed(breed);
        temp.setBid(bid);
        temp.setState(state);
        if(!lastoid.equals(0))//为0则为空
        {
            temp.setLastOid(lastoid);
        }
        temp.setAid(id);
        System.out.println("更新成功："+temp.toString());
        result.setDetail(animalService.update(temp));
        result.setSuccess(true);
        result.setMsg("更新牲畜信息成功");
        return result;
    }
    /**
     * 更新产品数据
     *
     * @param type 肉品类型
     * @param weight 重量
     * @param ptime 加工时间
     * @param lastOid 最新订单号
     * @param state 状态
     * @return Result对象
     */
    public Result UpdateProduct(Integer pid,Integer type, Double weight, String ptime,Integer lastOid,Short state) {
        Result result=new Result();
        result.setSuccess(false);
        if(productService.queryById(pid)==null){
            result.setMsg("产品id错误");
            return result;
        }
        Product product=new Product();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(ptime);
        } catch (ParseException e) {
            System.out.println("时间输入格式错误");
            result.setMsg("时间输入格式错误");
            return result;
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
        }
        product.setPid(pid);
        product.setType(type);
        product.setWeight(weight);
        product.setPtime(ptime);
        product.setLastOid(lastOid);
        product.setState(state);
        result.setSuccess(true);
        result.setMsg("更新产品信息成功");
        result.setDetail(productService.update(product));
        return result;
    }
    /**
     * 更新饲养员数据
     * @param bid 饲养员id
     * @param bname 饲养员名字
     * @param  eid 企业id
     * @return 单条数据
     */
    public Result UpdateBreeder(Integer bid,String bname,Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        if (breederService.queryById(bid)==null){
            result.setMsg("饲养员id错误");
            return result;
        }
        if (entService.queryById(eid)==null){
            result.setMsg("企业id错误");
            return result;
        }
        Breeder breeder=new Breeder();
        breeder.setBid(bid);
        breeder.setEid(eid);
        breeder.setBname(bname);
        result.setDetail(breederService.update(breeder));
        result.setSuccess(true);
        result.setMsg("更新饲养员信息");
        return result;
    }
    /**
     * 更新牲畜经营者数据
     * @param bid 牲畜经营者id
     * @param bname 牲畜经营者名字
     * @param  eid 企业id
     * @return Result对象
     */
    public Result UpdateButcher(Integer bid, String bname, Integer eid){
        Result result=new Result();
        result.setSuccess(false);
        if(butcherService.queryById(bid)==null){
            result.setMsg("经营者id错误");
            return result;
        }
        if(entService.queryById(eid)==null){
            result.setMsg("企业id错误");
            return result;
        }
        Butcher butcher=new Butcher();
        butcher.setBid(bid);
        butcher.setBname(bname);
        butcher.setEid(eid);
        result.setDetail(butcherService.update(butcher));
        result.setMsg("经营者信息更新成功");
        return result;
    }


}
